package personal.gavin.gtweet.widget
{
	import flash.events.EventDispatcher;
	
	import personal.gavin.gtweet.event.LoadFinishEvent;
	import personal.gavin.gtweet.event.ProcessXMLFinishEvent;
	
	public class ProfileUtil
	{
		public function ProfileUtil(){
			
		}
		
		/**
		 * profile dispatcher 
		 */		
		private var _profileDispatcher:EventDispatcher = new EventDispatcher();
		
		public function getProfileById(id:Number, callback:Function):void {
			var url:String = Setting.api_url + "users/show.xml?id=" + id;
			getProfileByUrl(url,callback);
		}
		
		public function getProfileByName(screen_name:String, callback:Function):void {
			var url:String = Setting.api_url + "users/show.xml?id=" + screen_name;
			getProfileByUrl(url,callback);
		}
		
		/**
		 * connect api server to get profile data 
		 * @param url profile url
		 */		
		public function getProfileByUrl(url:String, callback:Function):void {
			this._profileDispatcher.addEventListener(ProcessXMLFinishEvent.PROCESS_XML_FINISHED, callback);
			var connection:URLRequestHelp = new URLRequestHelp(url);
			connection._eventDispatcher.addEventListener(LoadFinishEvent.LOADFINISHED,doProcessXML);
			connection.load();
		}
		
		private function doProcessXML(net_finish:LoadFinishEvent):void{
			var xml_data:String = net_finish.data;			
			var _profile:Profile;
			if (net_finish.status != 200){
				_profile = new Profile();
				_profile.error = new Error(net_finish.status.toString() + " error");
			} else {
				_profile = ProfileUtil.convertStringtoProfile(xml_data);
			}
			var _process_event:ProcessXMLFinishEvent = new ProcessXMLFinishEvent(ProcessXMLFinishEvent.PROCESS_XML_FINISHED,_profile);
			this._profileDispatcher.dispatchEvent(_process_event);
		}
		
		public static function diffProfile(new_profile:Profile, old_profile:Profile):Profile {
			var different:Profile = new Profile();
			if ( old_profile == null || new_profile.error != null){
				return new_profile;
			}
			/**
			 * basic information
			 */
			different.id = new_profile.id != old_profile.id ? new_profile.id : -1;
			different.name = new_profile.name != old_profile.name ? new_profile.name : null;
			different.screen_name = new_profile.screen_name != old_profile.screen_name ? new_profile.screen_name : null;
			different.location = new_profile.location != old_profile.location ? new_profile.location : null;
			different.description = new_profile.description != old_profile.description ? new_profile.description : null;
			
			/**
			 * urls 
			 */		
			different.profile_image_url = new_profile.profile_image_url != old_profile.profile_image_url ? new_profile.profile_image_url : null;
			different.blog_url = new_profile.blog_url != old_profile.blog_url ? new_profile.blog_url : null;
			different.profile_background_image_url = 
				new_profile.profile_background_image_url != old_profile.profile_background_image_url ? new_profile.profile_background_image_url : null;
			
			/**
			 * basic statistic 
			 */		
			different.followers_count = new_profile.followers_count != old_profile.followers_count ? new_profile.followers_count : -1;
			different.statuses_count = new_profile.statuses_count != old_profile.statuses_count ? new_profile.statuses_count : -1;
			different.friends_count = new_profile.friends_count != old_profile.friends_count ? new_profile.friends_count : -1;
			
			/**
			 * theme and color by offical 
			 */
			var tempbg:uint = parseInt("0x"+new_profile.profile_background_color);
			if(tempbg != 0x9ae4e8){
				//0x9ae4e8 backgroud is not good~
				different.profile_background_color = 
				new_profile.profile_background_color != old_profile.profile_background_color ? new_profile.profile_background_color : null;
			} else {
				different.profile_background_color = "C3E0EE";
			}
			different.profile_text_color = 
				new_profile.profile_text_color != old_profile.profile_text_color ? new_profile.profile_text_color : null;
			different.profile_link_color = 
				new_profile.profile_link_color != old_profile.profile_link_color ? new_profile.profile_link_color : null;
			different.profile_sidebar_fill_color = 
				new_profile.profile_sidebar_fill_color != old_profile.profile_sidebar_fill_color ? new_profile.profile_sidebar_fill_color : null;
			different.profile_sidebar_border_color = 
				new_profile.profile_sidebar_border_color != old_profile.profile_sidebar_border_color ? new_profile.profile_sidebar_border_color : null;
			
			return different;
		}
		
		public static function isNullProfile(_profile:Profile):Boolean {
			if ( _profile.error != null ) {
				return false;
			}
			/**
			 * basic information
			 */	
			if (_profile.id > -1 || _profile.name != null || _profile.screen_name != null 
			|| _profile.location != null || _profile.description != null) {
				return false;
			}
			/**
			 * urls 
			 */	
			if (_profile.profile_image_url != null || _profile.blog_url != null ||
			_profile.profile_background_image_url != null) {
				return false;
			}
						
			/**
			 * basic statistic 
			 */	
			if (_profile.followers_count > -1 || _profile.statuses_count > -1 ||
			_profile.friends_count > -1) {
				return false;
			}
			
			/**
			 * theme and color by offical 
			 */
			if (_profile.profile_background_color != null || _profile.profile_text_color != null ||
			_profile.profile_link_color != null || _profile.profile_sidebar_fill_color != null || 
			_profile.profile_sidebar_border_color != null ) {
				return false;
			}
			
			return true;
		}
		
		public static function convertStringtoProfile(data:String):Profile {
			var _profile:Profile = new Profile();
			
			if (data == null){
				_profile.error = new Error("Can't get profile");
				return _profile;
			}
			var xml_data:XML = new XML(data);
			trace(xml_data.FlashPlayer.toString());
			_profile = convertXMLtoProfile(xml_data);
			return _profile;
		}

		public static function convertXMLtoProfile(data:XML):Profile {
			var _profile:Profile = new Profile();
			
			_profile.xml_data = data;
			trace(_profile.xml_data.FlashPlayer.toString());
			
			var error:String = _profile.xml_data.child("error").toString();
			if ( error != ""){
				_profile.error = new Error(error);
			} else {
				/**
				 * basic information
				 */		
				_profile.id = parseInt(_profile.xml_data.child("id").toString());
				_profile.name = _profile.xml_data.child("name").toString();
				_profile.screen_name = _profile.xml_data.child("screen_name").toString();
				_profile.location = _profile.xml_data.child("location").toString();
				_profile.description = _profile.xml_data.child("description").toString();
				
				/**
				 * urls 
				 */		
				_profile.profile_image_url = _profile.xml_data.child("profile_image_url").toString();
				_profile.blog_url = _profile.xml_data.child("url").toString();
				_profile.profile_background_image_url = _profile.xml_data.child("profile_background_image_url").toString();
				
				/**
				 * basic statistic 
				 */		
				_profile.followers_count = parseInt(_profile.xml_data.child("followers_count").toString());
				_profile.statuses_count = parseInt(_profile.xml_data.child("statuses_count").toString());
				_profile.friends_count = parseInt(_profile.xml_data.child("friends_count").toString());
				
				/**
				 * theme and color by offical 
				 */
				var tempbg:uint = parseInt("0x"+_profile.xml_data.child("profile_background_color").toString());
				if(tempbg != 0x9ae4e8){
					_profile.profile_background_color = _profile.xml_data.child("profile_background_color").toString();
				}else {
					_profile.profile_background_color = "C3E0EE";
				}
				_profile.profile_text_color = _profile.xml_data.child("profile_text_color").toString();
				_profile.profile_link_color = _profile.xml_data.child("profile_link_color").toString();
				_profile.profile_sidebar_fill_color = _profile.xml_data.child("profile_sidebar_fill_color").toString();
				_profile.profile_sidebar_border_color = _profile.xml_data.child("profile_sidebar_border_color").toString();
			}
			return _profile;
		}
	}
}